package com.hsd.znsh.vlc;

import com.hsd.znsh.utils.PortUtil;
import com.hsd.znsh.utils.VlcUtil;

import javax.servlet.ServletContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-28 11:28:01
 */
public class VlcListenerThread extends Thread {

    public boolean flag=true;

    private ServletContext servletContext;

    public VlcListenerThread(ServletContext servletContext){
        this.servletContext=servletContext;
    }

    @Override
    public void run() {
        Map<Integer,VlcThread> map;
        Set<Integer> set;
        while(flag){
            try {
                Thread.sleep(30*10*6*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            map=VlcUtil.getMap(servletContext);
            set=map.keySet();
            List<Integer> list=new ArrayList<>();
            for(Integer integer:set){
                System.out.println(integer);
                if(!PortUtil.findPortInUsing(8080+integer)){
                    System.out.println(integer);
                    list.add(integer);
                }
            }
            for(Integer integer:list){
                VlcUtil.endVlc(servletContext,integer);
            }
        }
        map=VlcUtil.getMap(servletContext);
        set=map.keySet();
        for(Integer integer:set){
            VlcUtil.endVlc(servletContext,integer);
        }
        System.out.println("VlcListenerThread end");
    }


}
